﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace LanguageAnalyse
{
    public partial class LanguageCompare : Form
    {
        public LanguageCompare()
        {
            InitializeComponent();
        }
        List<LangItem> LangItemListCN = new List<LangItem>();
        List<LangItem> LangItemListTW = new List<LangItem>();
        List<LangItem> LangItemListEN = new List<LangItem>();


        List<MenuName> ListMenu = new List<MenuName>();


        private void LoadLanguage(List<LangItem> LangItemList, string fileName)
        {
            if (!File.Exists(fileName))
            {
                MessageBox.Show("文件不存在！");
                return;
            }
            //開始

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(fileName);

            XmlNode root = xmlDoc.DocumentElement;

            XmlNodeList list1 = root.SelectNodes("aplanguageb");

            foreach (XmlNode node1 in list1)
            {
                LangItem item1 = new LangItem();
                item1.lgtype = node1.SelectSingleNode("lgtype").InnerText;
                item1.lgkey = node1.SelectSingleNode("lgkey").InnerText;
                item1.lgvalue = node1.SelectSingleNode("lgvalue").InnerText;
                item1.pid = node1.SelectSingleNode("pid").InnerText;
                item1.pgid = node1.SelectSingleNode("pgid").InnerText;
                item1.gid = node1.SelectSingleNode("gid").InnerText;
                item1.fieldname = node1.SelectSingleNode("fid").InnerText;
                item1.tablename = node1.SelectSingleNode("tablename").InnerText;

                LangItemList.Add(item1);
            }
           
        }

        private void LoadCN_Click(object sender, EventArgs e)
        {
            DirectoryInfo dir = new DirectoryInfo(txtPath.Text);
            if (!dir.Exists)
            {
                MessageBox.Show("目錄不存在！");
                return;
            }

            foreach (FileInfo file in dir.GetFiles())
            {
                var LanguageItemList= getLanguageItem(file.Name);
                LoadLanguage(LanguageItemList, file.FullName);
            }
            MessageBox.Show("裝載完成！");
        }

        private void btnCompare_Click(object sender, EventArgs e)
        {
            if (chklstLeft.CheckedItems.Count <= 0 || chklstRight.CheckedItems.Count <= 0)
            {
                MessageBox.Show("未选择比较项！");
                return;
            }


            string leftLanguage = chklstLeft.CheckedItems[0].ToString();
            string rightLanguage = chklstRight.CheckedItems[0].ToString();

            List<LangItem> LangItemLeft = getLanguageItem(leftLanguage);
            List<LangItem> LangItemRight = getLanguageItem(rightLanguage);

            compareLanguage(LangItemLeft, LangItemRight);

        }

        private List<LangItem> getLanguageItem(string key)
        {
            switch (key)
            {
                case "簡體":
                case "zh-cn.xml":
                    return LangItemListCN;
                case "繁體":
                case "zh-tw.xml":
                    return LangItemListTW;
                case "英文":
                case "en-us.xml":
                    return LangItemListEN;
            }
            return null;
        }




        private void compareLanguage(List<LangItem> LeftLang, List<LangItem> RightLang)
        {
            string pgid=txtPdId.Text.Trim();



            //LangItem aa = LeftLang.Single(m => m.lgkey == "FD1_PEF0204bg_cprice");

            //LangItem bb = LeftLang.Single(m => m.lgkey == "FD1_PEF0204bg_cprice");


            var coll = (from itemLeft in LeftLang
                        join itemRight in RightLang on itemLeft.lgkey equals itemRight.lgkey into ABC
                        from itemRight in ABC.DefaultIfEmpty()

                        where (itemLeft.lgvalue == "" || (itemRight != null ? itemRight.lgvalue : "") == "")
                        select new
                        {
                            Pgid = itemLeft.pgid,
                            Pid = itemLeft.pid,
                            Key = itemLeft.lgkey,
                            LeftValue = itemLeft.lgvalue,
                            RightValue = itemRight != null ? itemRight.lgvalue : "N/A",
                            gid = itemLeft.gid,
                            field = itemLeft.fieldname,
                            tablename = itemLeft.tablename
                        }).
                           Union(
                           from itemRight in RightLang
                           join itemLeft in LeftLang on itemRight.lgkey equals itemLeft.lgkey
                           into Coll
                           from itemLeft in Coll.DefaultIfEmpty()
                           where itemLeft == null
                           select new
                           {
                               Pgid = itemRight.pgid,
                               Pid = itemRight.pid,
                               Key = itemRight.lgkey,
                               LeftValue = "N/A",
                               RightValue = itemRight != null ? itemRight.lgvalue : null,
                               gid = itemRight.gid,
                               field = itemRight.fieldname,
                               tablename = itemRight.tablename
                           });

            //var coll = from itemLeft in LeftLang
            //           join itemRight in RightLang on itemLeft.lgkey equals itemRight.lgkey into ABC
            //           from itemRight in ABC.DefaultIfEmpty()

            //           where (itemLeft.lgvalue == "" || (itemRight != null ? itemRight.lgvalue : "") == "")
            //           select new { Pgid = itemLeft.pgid, Pid = itemLeft.pid, Key = itemLeft.lgkey, LeftValue = itemLeft.lgvalue, RightValue = itemRight != null ? itemRight.lgvalue : null, gid= itemLeft.gid, field= itemLeft.fieldname, tablename=itemLeft.tablename };
            
            
            
            //if (pgid != "")
            //{
            //    coll = from itemLeft in LeftLang
            //               join itemRight in RightLang on itemLeft.lgkey equals itemRight.lgkey into ABC
            //               from itemRight in ABC.DefaultIfEmpty()

            //           where (itemLeft.lgvalue == "" || (itemRight != null ? itemRight.lgvalue : "") == "") 
            //                && itemLeft.pgid == pgid
            //           select new { Pgid = itemLeft.pgid, Pid = itemLeft.pid, Key = itemLeft.lgkey, LeftValue = itemLeft.lgvalue, RightValue = itemRight != null ? itemRight.lgvalue : null, gid = itemLeft.gid, field = itemLeft.fieldname, tablename = itemLeft.tablename };
            //}


            dataGridView1.DataSource = coll.ToList();
            dataGridView1.Columns[3].HeaderText = chklstLeft.CheckedItems[0].ToString();
            dataGridView1.Columns[4].HeaderText = chklstRight.CheckedItems[0].ToString();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            txtPath.Text= folderBrowserDialog1.SelectedPath;
        }

        public void LoadMenu()
        {
            ListMenu.Add(new MenuName { code = "權限管理", name = "POP" });
            ListMenu.Add(new MenuName { code = "簽核管理", name = "APP" });
            ListMenu.Add(new MenuName { code = "基準管理", name = "BAS" });
            ListMenu.Add(new MenuName { code = "材料清單", name = "BOM" });
            ListMenu.Add(new MenuName { code = "訂單管理", name = "POR" });
            ListMenu.Add(new MenuName { code = "採購管理", name = "MPU" });
            ListMenu.Add(new MenuName { code = "庫存管理", name = "MIN" });
            ListMenu.Add(new MenuName { code = "會員管理", name = "MEM" });
            ListMenu.Add(new MenuName { code = "連鎖總部管理", name = "SCO" });
            ListMenu.Add(new MenuName { code = "賬款管理", name = "INC" });
            ListMenu.Add(new MenuName { code = "會計管理", name = "ACT" });
            ListMenu.Add(new MenuName { code = "票據管理", name = "AST" });
            ListMenu.Add(new MenuName { code = "固定資產", name = "ASS" });
            ListMenu.Add(new MenuName { code = "發票管理", name = "INV" });
            ListMenu.Add(new MenuName { code = "薪資管理系統", name = "PR" });
            ListMenu.Add(new MenuName { code = "客戶關係", name = "CRM" });
            ListMenu.Add(new MenuName { code = "業務績效管理", name = "PEF" });
            ListMenu.Add(new MenuName { code = "網站基本資料", name = "WEB" });
            ListMenu.Add(new MenuName { code = "經銷網管理", name = "STO" });
            ListMenu.Add(new MenuName { code = "暢貨中心管理", name = "MAL" });
            ListMenu.Add(new MenuName { code = "超級商城管理", name = "SHO" });
            ListMenu.Add(new MenuName { code = "外賣網", name = "WMW" });
            ListMenu.Add(new MenuName { code = "人才資料管理", name = "ORG" });

        }

        private void LanguageCompare_Load(object sender, EventArgs e)
        {

        }


    }

    public class LangItem
    {

        public string lgtype;
        public string lgkey;
        public string lgvalue;
        public string pid;
        public string pgid;
        public string gid;
        public string fieldname;
        public string tablename;
        
        //lgtype      >en-us</lgtype>
        //lgkey           >PG_SAL_SAL0310</lgkey>
        //lgvalue         >業務員出勤統計分析表</lgvalue>
        //pid         >SAL</pid>
        //pgid            >SAL0310</pgid>
        //gid          />
        //fid              />
        //tablename       >apmodel</tablename>
    }

    public class MenuName
    {
        public string code;
        public string name;
    }

}
